#include <cstdlib>
#include <cmath>
#include <cstdio>

//#include <iostream>

#define xsize 352
#define ysize 288

//using namespace std;

void thresh(unsigned short *image, unsigned short*imm){ //image[xsize*ysize]){
//      short *imm[xsize*ysize/16];
        unsigned short temp1,temp2;
        short thresh;
        long threshl;
        //short bit;
        short max=0;
        long sum=0;
        
        short bitcount=0;
        short shortcount=0;
        unsigned short shorttemp;
        unsigned int i;
        unsigned short temp_everything[xsize*ysize];
        long redsum=0;
        long greensum=0;
        short r;
        short g;
        //short stupid_temp;

        for(i=0;i<(xsize*ysize/2);i++)
        {
                //stupid_temp = (*(image+i));
//              cout<<"ANNA BANANA" << endl;

               // temp1=(((*(image+i))>>8)&3)*14-(((*(image+i))>>10)&7)*3-(((*(image+i))>>13)&7)*3;               //8 most significant bits               
               // temp2=(((*(image+i)))&3)*14-(((*(image+i))>>2)&7)*3-(((*(image+i))>>5)&7)*3;                    //8 lower bits
                temp1=((*(image+i))>>8)&255; //getting pixel 1
                temp2=(*(image+i))&255; //getting pixel 2
                redsum+=((temp1>>5)&7)+((temp2>>5)&7); //keeping a running sum, over all the pixels, of the total amount of red
                greensum+=((temp1>>2)&7)+((temp2>>2)&7);//" " of the total amount of green
//              cout << "JOSH!!!"<<endl;

                sum = sum+temp1+temp2; //summing the value of each pixel to get a "brightness" value for the whole image
                //redsum+=(temp1>>5)&7; // ERROR!!! Why is this begin done again? This was done on line 41-42???
                //greensum+=(temp1>>2)&7;//ERROR!!! Why is this begin done again? This was done on line 41-42???
                //redsum+=(temp2>>5)&7; // ERROR!!! Why is this begin done again? This was done on line 41-42???
                //greensum+=(temp2>>2)&7;// ERROR!!! Why is this begin done again? This was done on line 41-42???
                //temp_everything[2*i]=temp1; //storing the entire pixel in a temp array

//              cout << "ESTHER IS PIMP"<<endl;
                temp_everything[2*i+1]=temp2; //storing the entire pixel in a temp array

//              cout <<"I LOVE ROBOTS"<<endl;
				// Keeping track of what the value of the brightest pixel is
                if (temp1>max){
                        max = temp1;
                }
                else if (temp2>max){
                        max = temp2;
                }
        }

        threshl=sum/xsize/ysize; //average brightness of current frame
        redsum=redsum/xsize/ysize; //average red of current frame
        greensum=greensum/xsize/ysize; //average green of current frame
        thresh=(short)threshl; 
        thresh=(thresh+max)/2; // midpoint(value of brightest pixel, average brightness)
        r=(short)redsum;
        g=(short)greensum;

        for(i=0;i<(xsize*ysize);i++)
		{
        		if( (((temp_everything[i]>>5)&7)<r)&&(((temp_everything[i]>>2)&7)<g)&&((temp_everything[i]&3)>0))
				{
                //temp=(image[i]&3)*14-((image[i]>>2)&7)*3-((image[i]>>5)&7)*3;
               // if((((temp_everything[i])&3)*7-(((temp_everything[i])>>2)&7)*3- \
               // (((temp_everything[i])>>5)&7)*3)>5){ 
                //if (temp_everything[i]>=thresh){
               // if (((temp_everything[i]&3)>=1)&&((((temp_everything[i])>>2)&7)<=3)&&((((temp_everything[i])>>5)&7)<=3)){
                        shorttemp+=1<<(15-bitcount);
                }
        //      cout<<"DO THE ROBOT!!!! DO THE MONKEY!!!" << endl;
                if (bitcount==15){
                        bitcount=0;
                        imm[shortcount]=shorttemp;
                        shortcount++;
                        shorttemp=0;
                }
                else{
                        bitcount++;
                }
        //      cout <<"NO PROGRAM!!"<<endl;
        }       
}

/*
for testing
int main(){
        FILE * fid;
        FILE * fid2;    
        fid = fopen("pimpREAL.txt","r");
        fid2 = fopen("pimpBINARY.txt","w");

        int i;
        unsigned short *image; //[xsize*ysize/2];
        unsigned short *imm; //[xsize*ysize/16];
        unsigned short img1[xsize*ysize/2];
        unsigned short imm1[xsize*ysize/16];
        void thresh(unsigned short *image, unsigned short*imm);
        //short tmp;

        image = &img1[0];
        imm = &imm1[0];

        //cout << "USELESS AND DISAPPOINTING" << endl;
        for (i=0;i<(xsize*ysize/2);i++){
        //      cout<<i<<endl;
                fscanf(fid, "%u \n", image+i);
                //*(image+i)=tmp;       
        }
        //cout <<"CONGRADULATIONS YOU JUST WON THE JACKPOT"<< endl;
        thresh(image,imm);
        
        for(i=0;i<(xsize*ysize/16);i++){
                fprintf(fid2, "%u \n", *(imm+i));       
        }
        fclose(fid);
        fclose(fid2);

        return 1;
}



*/